package com.xiaoxiong.gobang.evaluate;

import android.util.Log;

import com.xiaoxiong.gobang.bean.Player;

/**
 * @author xiaoxiong
 * @Package com.xiaoxiong.gobang.evaluate
 * @Description:
 * @email 249900679@qq.com
 * @date 2016/4/1 17:51
 */
public class FirstEvaluate implements Evaluator {

    private static int f[] = {1, 16, 17, 15};
    private int[] m_board;

    @Override
    public int Evaluate(int[] board, int position, int player) {
        Log.d("evaluate","评估一");
        m_board = board;
        return GetScore(position, player);
    }

    private int GetScore(int dis, int player) {
        int score = 0;
        for (int i = 0; i < f.length; ++i) {
            int c = 1;
            boolean nextLeft = false, nextRight = false;
            int d = dis + f[i];
            while (m_board[d] == player) {
                c++;
                d += f[i];
            }
            if (m_board[d] == Player.NULLPALYER) {
                nextLeft = true;
            }
            d = dis - f[i];
            while (m_board[d] == player) {
                c++;
                d -= f[i];
            }
            if (m_board[d] == Player.NULLPALYER) {
                nextRight = true;
            }
            //活棋
            if (nextLeft && nextRight) {
                if (c == 4) {
                    return 99999999;
                }
                switch (c) {
                    case 3:
                        if (score < 9999999) {
                            score = 9999999;
                        }
                        break;
                    case 2:
                        if (score < 99999) {
                            score = 99999;
                        }
                        Log.d("huo-chong",c + "活" + player);
                        break;
                }
            } else if ((nextLeft && !nextRight) || (!nextLeft && nextRight)) {
                //冲棋
                switch (c) {
                    case 4:
                        if (score < 666666) {
                            score = 666666;
                        }
                        break;
                    case 3:
                        if (score < 9999) {
                            score = 9999;
                        }
                        break;
                    case 2:
                        if (score < 999) {
                            score = 999;
                        }
                        break;
                    case 1:
                        if (score < 99) {
                            score = 99;
                        }
                        break;
                }
            }
        }
        return score;
    }
}
